package mysql.cs.packet;

import common.Util.ByteUtil;
import mysql.MysqlPacket;
import mysql.cs.prop.Capabilities;

import java.io.IOException;

/**
 * Type	Name	Description
 * int<1>	header	[00] or [fe] the OK packet header
 * int<lenenc>	affected_rows	affected rows
 * int<lenenc>	last_insert_id	last insert-id
 * if capabilities & CLIENT_PROTOCOL_41 {
 *   int<2>	status_flags	Status Flags
 *   int<2>	warnings	number of warnings
 * } elseif capabilities & CLIENT_TRANSACTIONS {
 *   int<2>	status_flags	Status Flags
 * }
 * if capabilities & CLIENT_SESSION_TRACK {
 *   string<lenenc>	info	human readable status information
 *   if status_flags & SERVER_SESSION_STATE_CHANGED {
 *     string<lenenc>	session_state_changes	session state info
 *   }
 * } else {
 *   string<EOF>	info	human readable status information
 * }
 * @ClassName OKPacket
 * @Description TODO
 * @Author fengk
 * @Date 2018/8/27
 */

public class OKPacket extends MysqlPacket {
    public byte header;
    public byte[] affectedRows;
    public byte[] intertId;
    public int serverStatus;
    public int warningNo;
    public String message;

    public void endcode(byte[] data) throws IOException{
        int index=0;
        header=data[0];
        index++;
        affectedRows= ByteUtil.readBinaryCodedLengthBytes(data,index);
        index+=affectedRows.length;
        intertId=ByteUtil.readBinaryCodedLengthBytes(data,index);
        index+=intertId.length;
        if((Capabilities.CLIENT_PROTOCOL_41&Capabilities.CAPABILITIES) != (long)0x00){
            serverStatus=ByteUtil.readUnsignedShortLittleEndian(data,index);
            index+=2;
            warningNo=ByteUtil.readUnsignedShortLittleEndian(data,index);
            index+=2;
        }else if((Capabilities.CLIENT_TRANSACTIONS&Capabilities.CAPABILITIES)!=(long)0x00){
            serverStatus=ByteUtil.readUnsignedShortLittleEndian(data,index);
            index+=2;
        }
        message=new String(ByteUtil.readFixedLengthBytes(data,index,data.length-index));
    }
    public byte[] decode() throws IOException{
        return null;
    }

    public byte getHeader() {
        return header;
    }

    public void setHeader(byte header) {
        this.header = header;
    }

    public byte[] getAffectedRows() {
        return affectedRows;
    }

    public void setAffectedRows(byte[] affectedRows) {
        this.affectedRows = affectedRows;
    }

    public byte[] getIntertId() {
        return intertId;
    }

    public void setIntertId(byte[] intertId) {
        this.intertId = intertId;
    }

    public int getServerStatus() {
        return serverStatus;
    }

    public void setServerStatus(int serverStatus) {
        this.serverStatus = serverStatus;
    }

    public int getWarningNo() {
        return warningNo;
    }

    public void setWarningNo(int warningNo) {
        this.warningNo = warningNo;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
